// pages/discount/discount.js
const app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    urlimg: app.urlimg,
    data:"",
    head_num: 1,
    header: [
      { header: '个人立减金',num:1, },
      { header: '亲友立减金', num: 2, },
    ],
    // 个人立减金
    // id: 立减金id   money: 优惠金额   text:满减金额  start_time:开始时间   end_time:结束时间  isshow:是否过期  （0过期，1未过期）
    reduce_money_list:"",
    //亲友立减金
    // id: 立减金id   money: 优惠金额   text:满减金额  all:总数量   recovery:已领数量  ischange:是否过期 （0过期，1未过期）

  },

  //头部内容切换 
  head_click: function (e) {
    console.log(e);
    var that = this;
    var head_num = that.data.head_num;
    var index = e.currentTarget.dataset.index;
    lode(that, index, that.data.options)
    that.setData({
      head_num: index,
    })
  },
  // 查看亲友立减金的分享详情
  detail_click:function(e){
    wx.navigateTo({
      url: '/pages/reducemoney/detail/index?id=' + e.currentTarget.dataset.id+'&sign=' + e.currentTarget.dataset.sign,
    })
  },
  //立即使用立减金
  shortly_use:function(){
    var thar = this;
    // console.log('跳到首页');
    wx.navigateTo({
      url: this.data.data.url,
    })
  },
  // 邀请好友(分享)
  Invitation_click: function (e) {
    var that=this;
    var data={};
    data.from="menu";
    data.target = e.currentTarget.dataset.id;
    // that.onShareAppMessage(data);
  },
  //已过期
  early_click:function(){
    //提示信息
    wx.showToast({
      title: '该立减金已过期',
      icon: 'loading',
      duration: 1500,
    }) 
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    lode(that, '1', options)
  },

  // 猜你喜欢列表
  click_goods: function (e) {
    wx.navigateTo({
      url: '/pages/index/detail/index?id=' + e.currentTarget.id,
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    var that = this;
    that.setData({
      sign: e.target.dataset.sign,
    })

    var share_img = that.data.data.share_img;
    var imageUrl = that.data.urlimg + share_img.substring(1);

    return {
      imageUrl: imageUrl,
      title: that.data.data.title, // 标题
      path: '/pages/reducemoney/detail/index?id=' + that.data.data.id + '&sign=' + that.data.sign, // 分享后点击页面链接
    }
  }
})


function lode(that, num, options){
  console.log(options); 
  //立减金请求
  wx.request({
    url: app.url + 'api_reduce_money/reduce_money_list',
    data: {
      id: options.id,
      type: num, // 个人立减金
    },
    header: {
      'content-type': 'application/json',
      'token': wx.getStorageSync('token_key')
    }, success: function (res) {
      console.log(res.data.result);
      that.setData({
        data: res.data.result,
        reduce_money_list: res.data.result.reduce_money_list,
        'options.id': options.id,
        likegoods:res.data.result.goods,
        sign: (res.data.result.reduce_money_list != "" ? res.data.result.reduce_money_list[0].sign:""),
      })
    }
  })
}